package com.loongtech.bi.action.monitor;

import com.loongtech.bi.action.authManagement.ResultData;
import com.loongtech.bi.entity.system.EntityMonitorUserConfig;
import com.loongtech.bi.entity.system.EntityMonitorUserGroup;
import com.loongtech.bi.manager.system.BiMonitorUserGroupManager;
import com.loongtech.bi.manager.system.SyncInfoLogManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysProjectManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * Author: yuri
 * Description: 告警组别管理
 * Date: 17:11 2017/5/15
 */
@RestController
@Scope("prototype")
@RequestMapping("/monitor_user_group")
public class MonitorUserGroupAction implements Serializable {
    private static final long serialVersionUID = 1L;

    @Resource
    private BiMonitorUserGroupManager userGroupManager;
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private SyncInfoLogManager syncInfoLogManager;

    @RequestMapping(value = "users.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> rolelist(@RequestParam Integer groupId) {
        List<EntityMonitorUserConfig> list;
        try {
            list = userGroupManager.findByQuery("select distinct p.id, p.userName from EntityMonitorUserGroup r, EntityMonitorUserConfig p where r.userId = p.id and r.groupId = ? order by p.id asc", groupId);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), list);
    }

    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modify(@RequestParam Integer groupId, @RequestParam(value = "userIdList[]") Integer[] userIdList) {
        StringBuilder insertSQL = new StringBuilder("insert into bi_monitor_usergroup(userId,groupId) values ");
        try {
            userGroupManager.excuteUpdateByHql("delete from EntityMonitorUserGroup where groupId = ?", groupId);
            for (Integer userId : userIdList) {
                EntityMonitorUserGroup entity = new EntityMonitorUserGroup();
                entity.setGroupId(groupId);
                entity.setUserId(userId);
                userGroupManager.persist(entity);
                insertSQL.append("(").append(userId).append(",").append(groupId).append("),");
            }

            String insertSql = insertSQL.substring(0, insertSQL.length() - 1);
            StringBuilder updateSQL = new StringBuilder("delete from bi_monitor_usergroup where groupId = ").append(groupId).append(";").append(insertSql);
            Set<String> gameId = SysProjectManager.projectIdNameMap.keySet();
            for (String gid : gameId) {
                syncInfoLogManager.add(gid, updateSQL.toString());
            }

            sysLogManager.addLog(session.getUserName(), "增加[监控功能]分组用户关联信息成功.");
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }
}